You have created config model and test folders in the
previous part of the demo. Apart from the config
model and test folders, there are two important
files to add, index.js and app.js. In the screen, all
the required files and folders are implemented. Add the configuration
related to the database. The database.js file contains the respective code related
to database connectivity. The database.js file
connects to MongoDB, where MongoDB is
regarded as a database. Implement the logic, create a constant reference for the
Mongoose using Mongoose. Another constant reference is the environment that is
going to be passed through the environment variable using
Mongo_URI is process.env. This constant
reference process has a ready-made reference
provided by Node.js. For reference, the
environment provides all access to the
environmental variables. Create an error method,
export.connect. To establish the
database connectivity, use the Mongoose reference,
that is mongoose.connect. Inside reference provides
the code and the URL. That is mongodb; / / local host; 27017/ moviedb. The 27017 is the
default Mongo port. For the logic related to connectivity use another
arrow method on the console.log to display
the meaningful message that has successfully
connected to Movie Database. Implement catch to catch error
while connecting database. In case no connectivity
is established, the console.log method is
used with a message inside the quotes that the
database connection failed and the
application exists. Again, use console.log with reference to the error
object created before. To exit the application, use the command process.exit
and 1 as a parameter. Next is to create two models. The user.js contains the
user's information and the movie.js contains
the information related to the movie. All the attributes related
to the user come under the user model and attributes related to the movie come
under the movie model. Take the reference to Mongoose. Create the user schema
inside the database with attributes implementation,
that is new.mongoose.Schema. Specify the scheme
related attributes. Email has the type string, and unique is going to be true. The username and password
are both of type string. The last attribute is the watch
list having a type array. Now export the model class and schema through the
command module.exports equals to mongoose.model with the user and user schema
inside the brackets. Implementation of
the movie model is similar to the user model. Use movie schema in
place of user schema. The attributes under
the movie model are movieId having type string
and unique is true. The attributes, movieName and yearReleased are of string type. Now export the model using the command
module.exports equals to mongoose.model with the movie and movie schema
inside the brackets. Both the models related to the user and the movie
get implemented. To implement the main
code inside the app.js, open the app.js file. The required dependencies
need to be added. Take the direct environmental
reference, that is.env. Connect to the database
through the.connect command. After that, take note
of the references to express and jsonwebtoken. Now you will call the reference express and store in
the constant app. Use express.json command
by using the app constant. To have the reference
of user and movie give the constant user command where the user is stored
inside the model. Similarly, for the movie, give the constant
movie command where the movie is stored inside
the respective model. One more constant variable, that is the token key. To implement the logic
related to saving the movie, use app.post and movie
inside brackets. The command takes input in
JavaScript Object Notation or JSON format and stores the
movie details in the database. Provide attributes
such as movieId, movieName, and yearReleased. To check the availability of
the data, give a statement, if not movie Id, name and year released
and request the status. The statement returns
the status of 400 and shows all
input is required. Now, check for the
existing movie. Execution of command works in an asynchronous mode where
findOne is a method. You have taken the data inside
the existing movie folder, give a statement, if it's an existing movie. In the case of duplicate data, return the status to 409, and show the movie
already exists. Create a new database, name movie to save the movie
that consists of attributes, movie Id, movieName,
and yearReleased. In this way, the post method will work and the movie
data will be added. The post method will
return to 201 status, and show the movie
saved as the output. The catch part as used for the error with the
console.log command. Write the logic to fetch the movie details
under app.route movies in the bracket.get function with the
arrow function. Perform anonymous
implementation using movie.find with parameter
error and result. In case of an error, the response.send command
gives the error as an output. Else, the response.send
gives the result. Thank you.